package com.budweiser.user.controller;

import com.budweiser.commons.utils.TreeData;
import com.budweiser.commons.utils.Utils;
import com.budweiser.model.dict.Dict;
import com.budweiser.model.log.LogAnnotation;
import com.budweiser.model.page.Page;
import com.budweiser.model.result.ResponseBase;
import com.budweiser.user.dto.DictSelectTree;
import com.budweiser.user.service.DictService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dict")
public class DictController {
	@Autowired
	private DictService dictService;

	/***
	 * 字典首页左边字典树
	 * 
	 * @return
	 */
	@PostMapping("/findDictTree")
	public TreeData findDictTree() {
		return new TreeData().backData(dictService.findDictTree());
	}

	@GetMapping("/all")
	public Page<Dict> findRoles(@RequestParam Map<String, Object> params, String limit, String page) {
		params.put("start", page);
		params.put("length", limit);
		return dictService.findDicts(params);
	}

	@GetMapping("/selectDictTree")
	public List<DictSelectTree> selectDictTree() {
		List<DictSelectTree> dictSelectTrees = dictService.getSelectDictTree();
		List<DictSelectTree> firstmenuSelectTrees = dictSelectTrees.stream().filter(m -> m.getPid().equals(0l))
				.collect(Collectors.toList());
		firstmenuSelectTrees.forEach(select -> {
			setChildSelectTree(select, dictSelectTrees);
		});
		return firstmenuSelectTrees;
	}

	@LogAnnotation(module = "新增字典")
	@PostMapping("/save")
	@PreAuthorize("hasAuthority('back:dict:save')")
	public ResponseBase save(Dict dict) {
		dictService.save(dict);
		return new ResponseBase(200, "新增成功！", null);
	}

	@LogAnnotation(module = "修改字典")
	@PostMapping("/update")
	@PreAuthorize("hasAuthority('back:dict:update')")
	public ResponseBase update(Dict dict) {
		dictService.update(dict);
		return new ResponseBase(200, "修改成功！", null);
	}

	private void setChildSelectTree(DictSelectTree selectTree, List<DictSelectTree> dictSelectTrees) {
		List<DictSelectTree> child = dictSelectTrees.stream().filter(m -> m.getPid().equals(selectTree.getId()))
				.collect(Collectors.toList());
		if (!CollectionUtils.isEmpty(child)) {
			selectTree.setChildren(child);
			child.parallelStream().forEach(s -> {
				setChildSelectTree(s, dictSelectTrees);
			});
		}
	}

	@LogAnnotation(module = "删除字典")
	@PostMapping("/del")
	@PreAuthorize("hasAuthority('back:dict:del')")
	public ResponseBase del(String ids) {
		dictService.dateleIds(Utils.getLongs(ids));
		return new ResponseBase(200, "删除成功！", null);
	}

	/***
	 * 根据父类code查询Dict对象
	 * 
	 * @param code
	 * @return
	 */
	@PostMapping("/findDictByParentCode")
	public List<Dict> findDictByParentCode(String code) {
		return dictService.findDictByParentCode(code);
	}

	@PostMapping("/anon/getDictByCode")
	public Dict getDictByCode(@RequestBody String code) {
		return dictService.getDictByCode(code);
	}
	@PostMapping("/anon/getDictByid")
	public Dict getDictByid(@RequestBody Long id) {
		return dictService.getDictByid(id);
	}
}
